import { defineStore } from 'pinia'

export const useUserStore = defineStore('user', {
  state: () => ({
    token: localStorage.getItem('token') || '',
    username: localStorage.getItem('username') || '',
    role: localStorage.getItem('userRole') || '',
  }),
  
  actions: {
    setUserInfo(data) {
      this.token = data.token
      this.username = data.username
      this.role = data.role
      
      // 保存到本地存储
      localStorage.setItem('token', data.token)
      localStorage.setItem('username', data.username)
      localStorage.setItem('userRole', data.role)
    },
    
    clearUserInfo() {
      this.token = ''
      this.username = ''
      this.role = ''
      
      // 清除本地存储
      localStorage.removeItem('token')
      localStorage.removeItem('username')
      localStorage.removeItem('userRole')
    }
  },
  
  getters: {
    isLoggedIn: (state) => !!state.token,
    isTeacher: (state) => state.role === 'teacher',
    isStudent: (state) => state.role === 'student'
  }
}) 